package red.stu.pconstraint.pojo;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import red.stu.pconstraint.engine.action.Action;
import red.stu.pconstraint.util.StringUtils;

import java.util.List;

/**
 * 执行位置
 * @author yihrmc @stu.red
 * @date 2019/11/21 13:41
 */
@Getter
@Setter
@ToString
@Accessors(chain = true)
public class ExecutionLocation {

    public static final ExecutionLocation EMPTY = new ExecutionLocation();

    /**
     * 处理到节点
     */
    public static final int LOCATION_NODE = 0;

    /**
     * 处理到节点块
     */
    public static final int LOCATION_BLOCK = 1;

    /**
     * 处理到表达式
     */
    public static final int LOCATION_EXPRESSION = 2;

    /**
     * 处理到行为组
     */
    public static final int LOCATION_ACTIONS = 3;

    /**
     * 分支ID
     */
    @NotNull
    private String branchId;

    /**
     * 节点ID
     */
    @NotNull
    private String nodeId;

    // --------------------

    /**
     * 节点块位置
     */
    @Nullable
    private Integer nodeBlockIndex = null;

    /**
     * 表达式位置
     */
    @Nullable
    private Integer expressionIndex = null;

    /**
     * 缓存的行为组
     */
    @Nullable
    private List<Action> actions;

    public boolean equalsIgnoreNull(ExecutionLocation locationDO) {
        if (locationDO == null) {
            locationDO = EMPTY;
        }
        if (branchId != null && !branchId.equals(locationDO.getBranchId())) {
            return false;
        }
        if (nodeId != null && !nodeId.equals(locationDO.getNodeId())) {
            return false;
        }
        if (nodeBlockIndex != null && !nodeBlockIndex.equals(locationDO.getNodeBlockIndex())) {
            return false;
        }
        if (expressionIndex != null && !expressionIndex.equals(locationDO.getExpressionIndex())) {
            return false;
        }
        return true;
    }

    public int getLocation() {
        if (actions != null) {
            // 行为切换
            return LOCATION_ACTIONS;

        } else if (expressionIndex != null) {
            // 表达式切换
            return LOCATION_EXPRESSION;

        } else if (nodeBlockIndex != null) {
            // 节点块切换
            return LOCATION_BLOCK;

        } else if (nodeId != null) {
            // 节点切换
            return LOCATION_NODE;

        }
        throw new RuntimeException("data error");
    }

}
